<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_Advertise_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/advertise_entity', 'Advertise_Entity');
    }

    public function countUserAdById($userid, $oWhere=array())
    {
        $this->db->select('id', true);
        $this->db->from($this->table_advertise_name);
        if(count($oWhere)>0){
            $this->db->where($oWhere);
        } else {
            $this->db->where(array('user_id'=>$userid, 'deleted_at' => null));
        }
        return $this->db->count_all_results();
    }

    public function getUserAdById($userid, $oSelect=array(), $oWhere=array(), $limit=ADVERTISE_LIMIT, $offset=0)
    {
        if(count($oSelect)>0){
            $this->db->select(implode(',', $oSelect), true);
        } else {
            $this->db->select('id, position, anchor, content', true);
        }
        $this->db->from($this->table_advertise_name);
        if(count($oWhere)>0){
            $this->db->where($oWhere);
        } else {
            $this->db->where(array('user_id'=>$userid, 'deleted_at' => null));
        }
        $this->db->order_by('position', 'ASC');
        $this->db->order_by('order', 'ASC');
        $this->db->limit($limit);
        $this->db->offset($offset);
        $result = $this->db->get()->result('Advertise_Entity');
        return $result;
    }

    public function register($advertise){
        $this->db->trans_begin();
        $this->db->insert($this->table_advertise_temp_name, $advertise);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }
}
?>